package com.situ.skymallcartapi.config;


import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Configuration
public class Config {

    //拦截器，拦截feign的每一个调用，传递请求头
    @Bean
    public RequestInterceptor requestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate rt) {
                ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
                HttpServletRequest req = sra.getRequest();
                String jwt = req.getHeader("Authorization");
                rt.header("Authorization", jwt);
            }
        };
    }
}
